<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/14
 * Time: 上午9:32
 */

namespace App\Library\User;


use App\Library\AuthenticatedUser;
use App\Models\Item;
use App\Models\PostItem;
use App\Models\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

trait CollectTrait
{
    use AuthenticatedUser;
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $dataid = $request->input('dataid');
        $datatype = $request->input('datatype');

        $collect = $this->user()->collects()->where('dataid', $dataid)->where('datatype', $datatype)->first();
        if (!$collect) {
            if ($datatype == 'article') {
                //文章收藏
                $article = PostItem::find($dataid);
                if ($article) {
                    $collect = $this->user()->collects()->create([
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$article->title,
                        'image'=>$article->image,
                    ]);
                }
            }

            if ($datatype === 'item') {
                $item = Item::find($dataid);
                if ($item) {
                    $collect = $this->user()->collects()->create([
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$item->title,
                        'image'=>$item->thumb,
                    ]);
                }
            }

            if ($datatype == 'shop') {
                $shop = Shop::find($dataid);
                if ($shop) {
                    $collect = $this->user()->collects()->create([
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$shop->shop_name,
                        'image'=>$shop->logo,
                    ]);
                }
            }
        }

        return ajaxReturn(['collect'=>$collect]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $id = $request->input('id');
        $this->user()->collects()->where('id', $id)->delete();
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCollect(Request $request)
    {
        $id = $request->input('id');
        return ajaxReturn(['collect'=>$this->user()->collects()->find($id)]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCollectList(Request $request)
    {
        $offset = $request->input('offset');
        $count  = $request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $type = $request->input('type');

        $items = $this->user()->collects()->where('datatype', $type)
            ->offset($offset)->limit($count)->orderByDesc('id')
            ->get()->map(function ($collect){
                $collect->image = image_url($collect->image);
                $collect->created_at = date('Y-m-d H:i:s', $collect->created_at);
                return $collect;
            });

        return ajaxReturn(['items'=>$items]);
    }
}
